package apace.commons.io;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.io.DirectoryWalker;


/**
 * 删除给定文件夹下的文件,可以保留特定文件夹下的文件
 * @author liuzq
 *
 */
@SuppressWarnings("rawtypes")
class FileCleaner extends DirectoryWalker {

	public FileCleaner() {
		super();
	}

	@SuppressWarnings("unchecked")
	public List<File> clean(File startDirectory) {
		List<File> results = new ArrayList<File>();
		try {
			walk(startDirectory, results);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return results;
	}
    
protected boolean handleDirectory(File directory, int depth, Collection results) {
	// 此处配置某些文件夹下的文件不删除
	if ("新建文件夹".equals(directory.getName())) {
		directory.delete();
		return false;
	} else {
		return true;
	}
}

@SuppressWarnings({ "unchecked" })
	protected void handleFile(File file, int depth, Collection results) {
      file.delete();
      results.add(file);
    }
  }

public class DirectoryWalkerTest {
	@SuppressWarnings("rawtypes")
	public static void main(String[] args) {
		List results = new ArrayList();
		FileCleaner fc = new FileCleaner();
		results = fc.clean(new File("d:" + File.separator + "test"));
		for (int i = 0; i < results.size(); i++) {
			System.out.println(i + ":" + ((File) results.get(i)).getName());
		}
	}
}
